package main

import (
	"fmt"

	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

// var session = connectDB("mongodb://user1:ApuCRv2HuLMGgg8NNjPZ@mongo-ssp-traffic-review-1:1302,mongo-ssp-traffic-review-2:1301,mongo-ssp-traffic-review-3:1301/traffic-review-test")

// var session = connectDB("mongodb://127.0.0.1:27017/traffic-review-test")
type Person struct {
	ID string    `bson:"_id"`
	Xi int       `bson:"aa"`
	Yi int       `bson:"bb"`
	ZB Zhuangbei `bson:"zhuangbei"`
}
type Zhuangbei struct {
	Chide string `bson:"chide"`
	Hede  string `bson:"hede"`
}
type Person1 struct {
	ID string `bson:"_id"`
	Xi int    `bson:"aa"`
}

var (
	URL        = "127.0.0.1:27017"
	mgoSession *mgo.Session
	dataBase   = "amytest"
)

func getSession() *mgo.Session {
	if mgoSession == nil {
		var err error
		mgoSession, err = mgo.Dial(URL)
		if err != nil {
			panic("dial wrong")
		}
	}
	return mgoSession.Clone()
}
func withCollection(collection string, s func(*mgo.Collection) error) error {
	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C(collection)
	return s(c)
}
func AddPerson(p1 ...interface{}) error {
	query := func(c *mgo.Collection) error {
		return c.Insert(p1...)
	}
	return withCollection("person", query)
}
func UpsertPerson(id string, change interface{}) error {
	exop := func(c *mgo.Collection) error {
		_, err := c.UpsertId(id, bson.M{"$set": change})
		return err
	}
	err := withCollection("person", exop)
	return err
}
func UpdateIncPerson(id string, change interface{}) error {
	exop := func(c *mgo.Collection) error {
		_, err := c.Upsert(bson.M{"_id": id}, bson.M{"$inc": change})
		return err
	}
	err := withCollection("person", exop)
	return err
}
func FindPersonCount(chide string) int {

	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C("person")
	count, _ := c.Find(bson.M{"zhuangbei.hede": chide}).Count()
	return count
}
func FindPersonByChide(chide string, hede string) (results Person) {
	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C("person")
	err := c.Find(bson.M{"zhuangbei.chide": chide, "zhuangbei.hede": hede}).Sort("asdfadsf").One(&results)
	fmt.Println(err)
	return results
}

type Tmap struct {
	ID    bson.ObjectId   `bson:"_id"`
	F     map[string]bool `bson:"f"`
	OutId bson.ObjectId   `bson:"oid"`
}

func TmapUpsert(idHex string) {
	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C("tmap")
	var id bson.ObjectId
	if !bson.IsObjectIdHex(idHex) {
		id = bson.NewObjectId()
	} else {
		id = bson.ObjectIdHex(idHex)
	}
	outid := bson.NewObjectId()
	setOnInsert := bson.M{"oid": outid}
	_, err := c.Upsert(
		bson.M{"_id": id},
		bson.M{"$setOnInsert": setOnInsert, "$set": bson.M{"f.one": true}},
	)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(id)
}
func TInc() {
	session := getSession()
	defer session.Close()
	c := session.DB(dataBase).C("tmap")
	inc := bson.M{
		"f.one": 1,
		"f.two": 1,
	}

	err := c.Update(bson.M{}, bson.M{"$inc": inc})
	if err != nil {
		fmt.Println(err.Error())
	}
}
func main() {
	// TmapUpsert(os.Args[len(os.Args)-1])
	TInc()
}
